package com.aistar.controller;

import com.aistar.pojo.Customer;
import com.aistar.service.CustomerService;
import com.aistar.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;

@Controller
public class CustomerController {
    @Autowired
    private CustomerService customerService;

    //用户登录（带有自动登录功能）
    @PostMapping("/customer/login")
    @ResponseBody
    public ServerResponse login(Integer type, String account, String custPassword, String isAutoLogin, HttpServletRequest request ) throws ServletException, IOException {

        ServerResponse serverResponse = customerService.getByAccountAndPwd(type,account,custPassword);

          /*  1. 若isAutoLogin没有自动登录功能，service 执行登功能
            2. 若isAutoLogin选择了自动登录，cookie，reponse.add（cookie），service 执行登功能*/

        if (serverResponse.getStatus()==1){//登录成功
            request.getSession().setAttribute("loginCustomer",serverResponse.getData());
        }
        return  serverResponse;
    }

    @GetMapping("/customer/telno/{telno}")
    @ResponseBody
    public ServerResponse getbytelno(@PathVariable("telno") String telno) {
        ServerResponse serverResponse = customerService.getByTelno(Long.valueOf(telno));
        return serverResponse;
    }

    @PostMapping("/customer/code")
    @ResponseBody
    public ServerResponse getCode(String telno) {
        if (telno == null) {
            ServerResponse serverResponse = ServerResponse.getDataFailed("请输入手机号"); //0 手机号为空
            return serverResponse;
        } else {
            ServerResponse serverResponse = customerService.getByTelno(Long.valueOf(telno));
            if (serverResponse.getStatus() == 1) {//手机号已经注册过，不能再注册，即不调用短信接口
                return serverResponse;
            } else {
                //用户尚未注册，调用短信接口
                // String code =  GetMessageCode.getCode(telnoStr);
                String code = "666666";
                System.out.println("code" + code);
                ServerResponse serverResponseCode = ServerResponse.getDateSuccess(code);
                serverResponseCode.setStatus(2);
                return serverResponseCode;
            }
        }
    }

    @PostMapping("/customer/add")
    @ResponseBody
    public ServerResponse add(String custTelno,String custPassword){
        Customer customer = new Customer();
        customer.setCustTelno(Long.valueOf(custTelno));
        customer.setCustPassword(custPassword);
        customer.setCustRegistTime(new Date());//注册时间

        ServerResponse serverResponse =  customerService.add(customer);
        return serverResponse;
    }
}
